package com.pro.control;

import com.pro.entity.JsCarousel;
import com.pro.entity.JsonData;
import com.pro.service.AdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/Advertisement")
public class AdvertisementController {

    @Autowired
    private AdvertisementService service;

    /**
     * @ClassName JsCarousel
     * @Author 谭金诚
     * @Date 2021/11/7
     */
    @RequestMapping("/getAdvertisement")
    @ResponseBody
    public JsonData getAdvertisement(JsCarousel jsCarousel){
        List<JsCarousel> advertisement = service.getAdvertisement(jsCarousel);
        return advertisement != null ? JsonData.buildSuccess(advertisement) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName boolean
     * @Author 谭金诚
     * @Date 2021/11/8
     */
    @RequestMapping("/delAdvertisement")
    @ResponseBody
    public JsonData delAdvertisement(Integer carouselId){
        boolean b = service.delAdvertisement(carouselId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName map
     * @Author 谭金诚
     * @Date 2021/11/8
     */
    // 上传文件
    private static int i = 0;

    @RequestMapping(value = "/upImgs", method = RequestMethod.POST)
    @ResponseBody
    public JsonData upload(MultipartFile[] files, HttpSession session)
            throws IllegalStateException, IOException {
        String name = null;
        //加密算法
        UUID randomUUID = UUID.randomUUID();

        for (MultipartFile file : files) {
            // 获取文件上传的原始名字
            name = file.getOriginalFilename();

            String end = name.substring(name.indexOf("."));
            String path = null;
            if (file.getSize() > 0) {
                i++;
                name = i + randomUUID.toString() + end;
                // 上传文件存放的路径
                path = System.getProperty("user.dir")+"/src/main/resources/static/images";
                // 封装一个目录
                File f = new File(path, name);
                // 将上传的文件传入某个文件夹中
                file.transferTo(f);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        Map<String, Object> data = new HashMap<>();
        data.put("src", "/images/" + name);
        map.put("data", data);
        return map != null ? JsonData.buildSuccess(map): JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName JsCarousel
     * @Author 谭金诚
     * @Date 2021/11/8
     */
    @RequestMapping("/upAdvertisement")
    @ResponseBody
    public JsonData upAdvertisement(JsCarousel jsCarousel){
        boolean b = service.upAdvertisement(jsCarousel);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName JsCarousel
     * @Author 谭金诚
     * @Date 2021/11/8
     */
    @RequestMapping("/addAdvertisement")
    @ResponseBody
    public JsonData addAdvertisement(JsCarousel jsCarousel){
        boolean b = service.addAdvertisement(jsCarousel);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName updateAdvertisement
     * @Author 谭金诚
     * @Date 2021/12/14
     */
    @RequestMapping("/updateAdvertisement")
    @ResponseBody
    public JsonData updateAdvertisement(Integer carouselId){
        boolean b = service.updateAdvertisement(carouselId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

}
